package com.jiaquyun.jcps.ui.access

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.jakewharton.rxbinding2.widget.RxTextView
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.BaseAbstractFragment
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.BiFunction
import kotlinx.android.synthetic.main.fragment_access_login.*

class LoginFragment : BaseAbstractFragment() {
    override val layoutResId: Int
        get() = R.layout.fragment_access_login

    companion object {
        fun newInstance(): LoginFragment {
            return LoginFragment()
        }
    }

    override fun initView() {

        tvLoginPwdCode.setOnClickListener {
            //手机验证码登录(密码登录切换到验证码登录)
            clLoginPwd.visibility = View.GONE
            clLoginCode.visibility = View.VISIBLE
            etLoginPwdPhone.text.clear()
            etLoginPwdPassword.text.clear()
            cbLoginPwdEye.isChecked = false
        }
        tvLoginCodePwd.setOnClickListener {
            //账号密码登录(验证码登录切换到密码登录)
            clLoginCode.visibility = View.GONE
            clLoginPwd.visibility = View.VISIBLE
            etLoginCodePhone.text.clear()
            etLoginCodeCode.text.clear()
        }

        //显示密码
        cbLoginPwdEye.setOnCheckedChangeListener { _, isChecked ->
            etLoginPwdPassword.transformationMethod = if (isChecked)
                HideReturnsTransformationMethod.getInstance()
            else PasswordTransformationMethod.getInstance()
        }

        observableLoginPwd()
        observableLoginCode()
    }

    override fun onResume() {
        super.onResume()
        cbLoginPwdEye.isChecked = false
    }

    /**
     * 密码登录输入框监听
     * */
    private fun observableLoginPwd() {
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(etLoginPwdPhone)
        val observablePassword: Observable<CharSequence> =
            RxTextView.textChanges(etLoginPwdPassword)
        val observable: Disposable = Observable.combineLatest(observablePhone, observablePassword,
            BiFunction<CharSequence, CharSequence, Boolean> { t1, t2 ->
                t1.isNotEmpty() && t2.isNotEmpty()
            })
            .subscribe {
                tvLoginSubmit.isEnabled = it
            }
        dispose.add(observable)
    }

    /**
     * 验证码登录输入框监听
     * */
    private fun observableLoginCode() {
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(etLoginCodePhone)
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(etLoginCodeCode)
        val observable: Disposable = Observable.combineLatest(observablePhone, observableCode,
            BiFunction<CharSequence, CharSequence, Boolean> { t1, t2 ->
                t1.isNotEmpty() && t2.isNotEmpty()
            })
            .subscribe {
                tvLoginSubmit.isEnabled = it
            }
        dispose.add(observable)
    }
}